package edu.sysu.demo.petstore.dog.web.rest;

import edu.sysu.demo.petstore.dog.user.DuplicateUsernameException;
import org.springframework.hateoas.VndErrors;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author lijinting01
 */
@RestControllerAdvice
public class ClientErrorController {

  @ResponseStatus(HttpStatus.FORBIDDEN)
  @ExceptionHandler(UsernameNotFoundException.class)
  public VndErrors handle(UsernameNotFoundException exception) {
    return new VndErrors("User name not found", exception.getMessage());
  }

  @ResponseStatus(HttpStatus.UNAUTHORIZED)
  @ExceptionHandler(BadCredentialsException.class)
  public VndErrors handle(BadCredentialsException exception) {
    return new VndErrors("Invalid password", exception.getMessage());
  }


  @ResponseStatus(HttpStatus.BAD_REQUEST)
  @ExceptionHandler(DuplicateUsernameException.class)
  public VndErrors handle(DuplicateUsernameException exception) {
    return new VndErrors("Username already exists", exception.getMessage());
  }
}
